<?php

/**
 * This is the model class for table "tbl_task".
 *
 * The followings are the available columns in table 'tbl_task':
 * @property int $id
 * @property string $name
 * @property string $status 
 * @property-read string $label 
 * @property-read string $statusLabel
 * @property-read mixed $statusTime
 * @property-read boolean $isSubmitAble
 */
class Task extends CActiveRecord
{

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_task';
	}

	public function __get($name)
	{
		if ($name === 'data') {
			$value = $this->getAttribute($name);
			if (is_string($value)) {
				$value = empty($value) ? array() : unserialize($value);
				$this->setAttribute($name, $value);
				return $value;
			}
		}
		return parent::__get($name);
	}

	public function getIsSubmitAble()
	{
		$status = $this->getStatus();
		return $status === 'none' || $status === 'finished' || $status === 'failed';
	}

	public function getLabel()
	{
		$labels = self::taskLabels();
		return isset($labels[$this->name]) ? $labels[$this->name] : ucfirst($this->name);
	}

	public function getStatus()
	{
		return isset($this->data['status']) ? $this->data['status'] : 'none';
	}

	public function setStatus($value)
	{
		if ($value !== $this->getStatus()) {
			$this->setData('status', $value);
			if (is_string($value)) {
				$this->setData($value . '_time', time());
			}
			$this->save();
		}
	}

	public function getStatusLabel()
	{
		$labels = self::statusLabels();
		$status = $this->getStatus();
		return isset($labels[$status]) ? $labels[$status] : ucfirst($status);
	}

	public function getStatusTime($format = 'Y/m/d H:i:s')
	{
		$time_key = $this->getStatus() . '_time';
		if (isset($this->data[$time_key])) {
			return $format === null ? $this->data[$time_key] : date($format, $this->data[$time_key]);
		}
		return null;
	}

	public function getData($key)
	{
		$data = $this->data;
		return isset($data[$key]) ? $data[$key] : null;
	}

	public function setData($key, $value)
	{
		$data = $this->data;
		$data[$key] = $value;
		$this->setAttribute('data', $data);
	}

	/**
	 * Set some attributes before save
	 */
	protected function beforeSave()
	{
		if (is_array($this->data)) {
			$this->data = serialize($this->data);
		}
		return parent::beforeSave();
	}

	/**
	 * @return Task[] all the task to be run
	 */
	public function findAllToBeRun()
	{
		$models = array();
		foreach (self::model()->findAll() as $model) {
			if (!$model->isSubmitAble) {
				$models[] = $model;
			}
		}
		return $models;
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className = __CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * Find or create task model by name
	 * @param string $name
	 * @return Task
	 */
	public static function load($name)
	{
		$model = self::model()->findByAttributes(array('name' => $name));
		if ($model === null) {
			$model = new self();
			$model->name = $name;
			$model->data = array();
		}
		return $model;
	}

	public static function taskLabels()
	{
		return class_exists('TaskRouter') ? TaskRouter::allTask() : array();
	}

	public static function statusLabels()
	{
		return array(
			'none' => '无',
			'submit' => '待处理',
			'ongoing' => '处理中',
			'finished' => '已完成',
			'failed' => '已失败',
		);
	}
}
